<template>
	<div class="container add_edit">
		<el-form :model="form" ref="ruleForm" label-width="100px">
			<el-form-item label="小程序名称">
				<el-input v-model="form.appTitle"></el-input>
			</el-form-item>

			<el-form-item>
				<el-button class="confirm_btn" @click="submitForm('ruleForm')">提 交</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	const __name = 'ny-app-base-config';
	export default {
		data() {
			return {
				dataId: '',
				form: {
					appTitle: "",
				},
			};
		},
		methods: {
			async loadData() {
				await this.$db[__name].tofirst().then(res => {
					if (res.code == 200) {
						this.form = res.datas
						this.dataId = res.datas._id;
					}
				});
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						if (!this.dataId) {
							this.$db[__name].add(this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.loadData();
								}
							}).catch(err => {
								uni.showModal({
									content: err.message,
									showCancel: false
								})

							});
						} else {
							delete this.form['_id']
							this.$db[__name].update(this.dataId, this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.loadData();
								}
							}).catch(err => {
								uni.showModal({
									content: err.message,
									showCancel: false
								})

							})
						}
					}
				});
			},
		},
		created() {
			this.loadData();
		}
	};
</script>

<style></style>
